package com.ruoyi.goods.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.goods.config.DelayedConfig;
import com.ruoyi.goods.domain.BmInorderlist;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author {那蓝桉}
 * @Date: 2023/07/27/ 20:27
 * @description
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @PostMapping("/createOrder")
    public String createOrder(BmInorderlist order){
        //1.创建订单
        System.out.println("订单创建成功!");
        //订单里的json对象转字符串
        String s = JSON.toJSONString(order);
        System.out.println("s = " + s);
        return "订单创建成功";
    }

    @PostMapping("/sendMsg")
    public void deadLetterProducer(String message){
        rabbitTemplate.convertAndSend("X","XA","消息来自ttl为10s的队列："+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自ttl为40s的队列："+message);
    }
    @PostMapping("/sendMsg01")
    public void deadLetterProducer01(String message,String ttl){
        rabbitTemplate.convertAndSend("X","XC","消息来自ttl为"+ttl+"ms的队列："+message,msg -> {
            msg.getMessageProperties().setExpiration(ttl);
            return msg;
        });
    }
    @PostMapping("/sendDelayedMsg")
    public void sendDelayedMsg(String message,Integer ttl){
        rabbitTemplate.convertAndSend(DelayedConfig.DELAYED_EXCHANGE_NAME,DelayedConfig.DELAYED_ROUTING_KEY,"消息来自ttl为"+ttl+"ms的队列："+message, msg -> {
//            设置消息的延迟时长  单位：ms
            msg.getMessageProperties().setDelay(ttl);
            return msg;
        });
    }
}
